package com.xiaoshou.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Controller;

import com.xiaoshou.pojo.Question;
import com.xiaoshou.pojo.Questiontype;
import com.xiaoshou.pojo.User;
import com.xiaoshou.util.EncryptPass;
@Controller
@Results({ 
	@Result(name = "verifyFail",location="login.jsp",type="dispatcher") ,
	@Result(name = "verifySuccessAndAskQuestion",location="askquestion.jsp",type="dispatcher"),
	@Result(name="loginSuccess",location="index.action",type="redirectAction"),
	@Result(name="gotoSetpage",location="setuser.jsp",type="dispatcher"),
	@Result(name="logoutSuccess",location="index.action",type="redirectAction")
})
public class UserAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private User user;
	private String task;
	private List<Question> questionsList;
	private List<Questiontype> questiontypesList;
	
	private String oldpassword;
	private String newpassword;
	private String userId;

	@Override
	public String execute() throws Exception {
		return null;
	}
	
	@Action("logout")
	public String logoutUser(){
		user = (User) getFromSession("user");
		putInSession("user", null);
		return "logoutSuccess";
	}
	
	@Action("verifyOldpassword")
	public String verifyOldpasswordByString(){
		user = (User) getFromSession("user");
		if(EncryptPass.encryptPassTen(oldpassword).equals(user.getPassword())){
			try {
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setContentType("application/json;charset=utf-8");
				PrintWriter out = response.getWriter();
				out.print("true".toString());
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}else {
			try {
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setContentType("application/json;charset=utf-8");
				PrintWriter out = response.getWriter();
				out.print("false".toString());
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
	}
	
	@Action("updateUser")
	public String updateUser(){
		user = (User) getFromSession("user");
		if(user.getPassword().equals(EncryptPass.encryptPassTen(oldpassword))){
			getUserService().modifyUser(user,newpassword);
			try {
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setContentType("application/json;charset=utf-8");
				PrintWriter out = response.getWriter();
				out.print("true".toString());
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else {
			try {
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setContentType("application/json;charset=utf-8");
				PrintWriter out = response.getWriter();
				out.print("false".toString());
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	@Action("setUser")
	public String setUser(){
		user = getUserService().getUserDao().findById(userId);
		return "gotoSetpage";
	}

	@Action("verifyLogin")
	public String verifyLogin() {
		user = getUserService().verifyLogin(user);
		if(user==null){
			//
			try {
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setContentType("application/json;charset=utf-8");
				PrintWriter out = response.getWriter();
				out.print("用户名或密码错误！".toString());
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}else if(user!=null&&"askQuestion".equals(task)){
			putInSession("user", user);
			return "verifySuccessAndAskQuestion";
		}else if(user!=null&&"login".equals(task)){
			questionsList = getQuestionService().findQuestsList();
			questiontypesList = getQuestionTypeService().getQuestionTypeDao().findAll();
			putInSession("user", user);
			return "loginSuccess";
		}else if(user==null||(task==null||"".equals(task))){
			//登录失败
			putInSession("user", user);
			return "verifyFail";
		}
		return null;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getTask() {
		return task;
	}

	public void setTask(String task) {
		this.task = task;
	}

	public List<Question> getQuestionsList() {
		return questionsList;
	}

	public void setQuestionsList(List<Question> questionsList) {
		this.questionsList = questionsList;
	}

	public String getOldpassword() {
		return oldpassword;
	}
	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}
	public String getNewpassword() {
		return newpassword;
	}
	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}

	public List<Questiontype> getQuestiontypesList() {
		return questiontypesList;
	}

	public void setQuestiontypesList(List<Questiontype> questiontypesList) {
		this.questiontypesList = questiontypesList;
	}

}
